﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class admin_stop_addup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"] == "del")
                    {
                        delData();
                    }
                }
                bindDDL();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    this.hidId.Value = Request.QueryString["id"];
                    bindData();
                }
            }
        }

        private void bindDDL()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlComList.DataSource = dal.GetCommunity(int.Parse(Session["CommId"].ToString()), true);
            this.ddlComList.DataBind();

            this.ddlBuildList.DataSource = dal.GetBuild(int.Parse(this.ddlComList.SelectedValue),null,true);//?????
            this.ddlBuildList.DataBind();
        }

        private void delData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                if (dal.Del_StopView(int.Parse(Request.QueryString["id"])))
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("err");
                }
            }
            Response.End();
        }

        private void bindData()
        {
            if (this.hidId.Value != "0")   
            {
               
                DataAccess.PmData dal = new DataAccess.PmData();
                System.Data.DataTable dt = dal.Get_StopView(int.Parse(this.hidId.Value),null);
                if (dt.Rows.Count > 0)
                {
                    this.ddlComList.Items.FindByValue(dt.Rows[0]["Self_Community_Code"].ToString()).Selected = true;
                    this.ddlBuildList.Items.FindByValue(dt.Rows[0]["Self_Build_Code"].ToString()).Selected = true;
                    this.ddlSelf_Unit_Code.Items.FindByValue(dt.Rows[0]["Self_Unit_Code"].ToString()).Selected = true;
                    this.ddlU_Self_No.Items.FindByValue(dt.Rows[0]["U_Self_No"].ToString()).Selected = true;
                    this.txtSelf_Name.Text = dt.Rows[0]["Self_Name"].ToString();
                    this.txtU_OwnerName.Text = dt.Rows[0]["U_OwnerName"].ToString();
                    this.ddlStopArea.Items.FindByValue(dt.Rows[0]["StopArea"].ToString()).Selected = true;
                    this.txtStopName.Text = dt.Rows[0]["StopName"].ToString();
                    this.ddlState.Items.FindByValue(dt.Rows[0]["State"].ToString()).Selected = true;
                    this.txtBeginUse.Text = dt.Rows[0]["BeginUse"].ToString();
                    this.txtEndUse.Text = dt.Rows[0]["EndUse"].ToString(); 

                    this.txtPackDate.Text = dt.Rows[0]["PackDate"].ToString();
                    this.txtPayDate.Text = dt.Rows[0]["PayDate"].ToString();
                    this.txtHTEnd.Text = dt.Rows[0]["HTEnd"].ToString();
                    this.txtManagePrice.Text = dt.Rows[0]["ManagePrice"].ToString();
                    this.txtMLeasePrice.Text = dt.Rows[0]["MLeasePrice"].ToString(); 

                    this.CBoxShareFlag.Checked= Convert.ToBoolean(dt.Rows[0]["ShareFlag"]);

                    this.radMFreeY.Checked = Convert.ToBoolean(dt.Rows[0]["Mfree"]);
                    this.radMFreeN.Checked = !Convert.ToBoolean(dt.Rows[0]["Mfree"]);
                    this.radStateFlagY.Checked = Convert.ToBoolean(dt.Rows[0]["StateFlag"]);
                    this.radStateFlagN.Checked = !Convert.ToBoolean(dt.Rows[0]["StateFlag"]);

                 }
            }
        } 

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string Self_Community = this.ddlComList.SelectedValue;
            string Self_Build = this.ddlBuildList.SelectedValue;
            string Self_Unit_Code = this.ddlSelf_Unit_Code.SelectedValue;
            string U_Self_No = this.ddlU_Self_No.SelectedValue;
            string Self_Name = this.txtSelf_Name.Text.Trim();
            string U_OwnerName = this.txtU_OwnerName.Text.Trim();
            string StopArea = this.ddlStopArea.SelectedValue;
            string StopName = this.txtStopName.Text.Trim();
            string myState = this.ddlState.SelectedValue;
            string BeginUse = this.txtBeginUse.Text.Trim();
            string EndUse = this.txtEndUse.Text.Trim();
            bool p_MFree = this.radMFreeY.Checked; 
            bool p_StateFlag =this.radStateFlagY.Checked;
            bool p_ShareFlag =this.CBoxShareFlag.Checked; 
            string PackDate = this.txtPackDate.Text.Trim(); 
            string PayDate = this.txtPayDate.Text.Trim();
            string HTEnd = this.txtHTEnd.Text.Trim();

            DateTime Checkdate;
            if (!DateTime.TryParse(BeginUse, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的使用起始日期!"); return; }
            if (!DateTime.TryParse(EndUse, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的使用截止日期!"); return; } 
            if (!DateTime.TryParse(PackDate, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的合同签定日期!"); return; }
            if (!DateTime.TryParse(PayDate, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的交付使用日期!"); return; }
            if (!DateTime.TryParse(HTEnd, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的合同截止日期!"); return; }


            decimal ManagePrice = decimal.Parse(this.txtManagePrice.Text.Trim());
            decimal MLeasePrice = decimal.Parse(this.txtMLeasePrice.Text.Trim());
            string Remark = this.txtRemark.Text.Trim(); 

            DataAccess.PmData dal = new DataAccess.PmData();
            if (hidId.Value == "0")
            {
                if (dal.Add_face_m_stopview(int.Parse(Self_Community), int.Parse(Self_Build), int.Parse(Self_Unit_Code),int.Parse(U_Self_No),StopArea,StopName,Self_Name,
                                            U_OwnerName,myState,DateTime.Parse(BeginUse),DateTime.Parse(EndUse),DateTime.Parse(PackDate),p_MFree,Remark,p_StateFlag,
                                            DateTime.Parse(PayDate),DateTime.Parse(HTEnd),Session["Name"].ToString(),DateTime.Now,p_ShareFlag,MLeasePrice,ManagePrice))
               
                      
                
                {
                    ClientScript.RegisterStartupScript(typeof(string), "1", "<script>alert('保存成功!');window.opener.location.reload();</script>");
                } 
            }
            else
            {
                if (dal.Modify_face_m_stopview(int.Parse(hidId.Value),int.Parse(Self_Community), int.Parse(Self_Build), int.Parse(Self_Unit_Code), int.Parse(U_Self_No), StopArea, StopName, Self_Name,
                                            U_OwnerName, myState, DateTime.Parse(BeginUse), DateTime.Parse(EndUse), DateTime.Parse(PackDate), p_MFree, Remark, p_StateFlag,
                                            DateTime.Parse(PayDate), DateTime.Parse(HTEnd), Session["Name"].ToString(), DateTime.Now, p_ShareFlag, MLeasePrice, ManagePrice))
                {
                    ClientScript.RegisterStartupScript(typeof(string), "1", "<script>alert('修改成功！');window.opener.location.reload();</script>");
                }
            }
        }

    }
}